﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;


public class FollowAvatar
{
    public CameraModule Handle;
    private Transform transform;
    private Transform target;
    private float distance = 16.0f;
    private float Damping = 0.075f;
    private bool findTarget = false;
	private Vector3 defaultRot = new Vector3(15, 270, 0);
    private Vector3 adjustHeight = Vector3.up * .25f;
    private int layerMask = 1 << Define.TerrainAlphaLayer;
    public FollowAvatar(CameraModule handle)
    {
        Handle = handle;
        transform = Define.MainCamera.transform;
        RegionStateManager.LoginStateChangeHandle += delegate(LoginState form, LoginState to)
        {
            if(to == LoginState.LoginEnd)
            {
                target = AppInterface.AvatarModule.IOwner.Collider.transform;
                findTarget = true;
            }
            if (to == LoginState.LogoutEnd)
            {
                findTarget = false;
            }
        };
		transform.eulerAngles = defaultRot;
    }
    private void followAvatar()
    {
        if (findTarget) transform.position = Vector3.Lerp(transform.position + adjustHeight, target.position - Quaternion.Euler(defaultRot) * Vector3.forward * distance, Damping);
    }
    public void Update()
    {
        followAvatar();
    }
    public void FixedUpdate()
    {
        if (findTarget) hidenCover();
    }
    public void LateUpdate()
    {
        //followAvatar();
    }
    private Dictionary<string, AlphaGameObject> transObjects = new Dictionary<string, AlphaGameObject>();
    private RaycastHit hit;
    private const float minAlpha = .4f;
    private void hidenCover()
    {
        if (Physics.Raycast(transform.position, target.position - transform.position, out hit, 100, layerMask))
        {
            if (!transObjects.ContainsKey(hit.transform.name))
            {
                hit.transform.name = DateTime.Now.Ticks.ToString();
                transObjects.Add(hit.transform.name, new AlphaGameObject(hit.transform.gameObject, Color.white));
            }
        }
        foreach (KeyValuePair<string, AlphaGameObject> obj in transObjects)
        {
            if (Mathf.Abs(obj.Value.G.transform.position.z - target.position.z) < 5)
            {
                obj.Value.C.a -= .033f;
                if (obj.Value.C.a <= minAlpha) obj.Value.C.a = minAlpha;
                obj.Value.M.color = obj.Value.C;
            }
            else
            {
                obj.Value.C.a += .033f;
                if (obj.Value.C.a >= 1) obj.Value.C.a = 1;
                obj.Value.M.color = obj.Value.C;
            }
        }
    }
}
public class AlphaGameObject
{
    public AlphaGameObject(GameObject g, Color c)
    {
        G = g;
        C = c;
        M = G.renderer.material;
    }
    public GameObject G;
    public Color C;
    public Material M;
}

